package chen.huai.jie.spring.xmlconfig.mq.rabbitmq.sender;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RabbitMqSender {
    private Logger logger = LoggerFactory.getLogger(RabbitMqSender.class);

    @Autowired(required = false)
    private RabbitTemplate rabbitTemplate;

    public void sendMessageToQueue(String queueName, Object message) {
        rabbitTemplate.convertAndSend("", queueName, message);
        logger.info(String.format("Send message to %s queue successfully.", queueName));
    }

    public void sendMessageToExchange(String exchangeName, String routingKey, Object message) {
        rabbitTemplate.convertAndSend(exchangeName, routingKey, message);
        logger.info(String.format("Send message to %s exchange successfully.", exchangeName));
    }
}
